/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.games.esmi.ieat.indicatorvalues;

import com.vaadin.ui.Button.ClickEvent;
import java.awt.Color;
import com.vaadin.addon.timeline.Timeline;
import com.vaadin.addon.timeline.Timeline.ChartMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.VerticalLayout;
import eu.games.esmi.ieat.indicatorvalues.data.GPIValues;
import eu.games.esmi.ieat.indicatorvalues.data.IndicatorSeries;
import java.util.Date;
import java.util.List;

/**
 *
 * @author user
 */
public class IndicatorChart extends VerticalLayout implements Button.ClickListener {

    private String name;
    private Timeline timeline;
    private List<IndicatorSeries> series;
    CheckBox cbNormalized;
    Color[] colorList = {Color.BLUE, Color.RED, Color.GREEN, Color.ORANGE, Color.GRAY};
    float max = Float.MIN_VALUE;
    float min = Float.MAX_VALUE;
    boolean normalized = false;
    HorizontalLayout detailsBar = new HorizontalLayout();

 
    
    public IndicatorChart(String name, List<IndicatorSeries> series) {
        super();
        this.name = name;
        this.series = series;

        //this.setSizeFull();

        this.setMargin(true);
        this.setSizeFull();
        //((Layout)this.getContent()).setWidth("1000px");

        this.addStyleName("chart");

        
        this.initTimeLine();
       this.initDetails();
    }
    
         

    

    public void initTimeLine() {


//        VerticalLayout vl = new VerticalLayout();
//        vl.setSpacing(true);
//        vl.setStyleName("chart");
//        vl.setWidth("800px");
//        vl.setHeight("500px");
        timeline = new Timeline(this.name);
        timeline.setWidth("100%");
        timeline.setHeight("400px");

        timeline.setDebugId(this.name);
        timeline.setChartMode(ChartMode.LINE);
        //timeline.setVerticalAxisRange(0f, 100f);

        this.setSeries(series);

        cbNormalized = new CheckBox("normalized");
        cbNormalized.setImmediate(true);
        cbNormalized.addListener(this);



        this.addComponent(timeline);
        this.addComponent(cbNormalized);
        //((VerticalLayout)this.getContent()).setExpandRatio(timeline, 1.0f);

    }

    private void initDetails() {

        this.detailsBar.setStyleName("v-info-bar");
        this.detailsBar.setSpacing(true);




        this.addComponent(this.detailsBar);
    }

    public void setSeries(List<IndicatorSeries> series) {


        this.series = series;
        
        if (series != null) {

            //updating the timeline


            this.min = Float.MAX_VALUE;
            this.max = Float.MIN_VALUE;

            timeline.removeAllGraphDataSources();
            timeline.setChartMode(ChartMode.LINE);
            //timeline.setVerticalAxisRange(0f, 100f);

            timeline.addZoomLevel("1d", 86400000L);
            timeline.addZoomLevel("5d", 5 * 86400000L);
            timeline.addZoomLevel("1m", 2629743830L);
            timeline.addZoomLevel("3m", 3 * 2629743830L);
            timeline.addZoomLevel("6m", 6 * 2629743830L);
            timeline.addZoomLevel("1y", 31556926000L);
            timeline.addZoomLevel("Max", 5 * 31556926000L); //5 years should be enough
 


            int i = 0;
            for (IndicatorSeries is : series) {

                if (is.getMax() > max) {
                    max = is.getMax();
                }

                if (is.getMin() < min) {
                    min = is.getMin();
                }

                if (min > 0) {
                    min = 0;
                }

                if (normalized) {
                    timeline.addGraphDataSource(is.getNormalized());
                    timeline.setGraphLegend(is.getNormalized(), is.getName());
                    timeline.setGraphOutlineColor(is.getNormalized(), colorList[i]);
                    timeline.setGraphFillColor(is.getNormalized(), null);
                    timeline.setVerticalAxisLegendUnit(is.getNormalized(), is.getUnit());
                } else {
                    timeline.addGraphDataSource(is.getValues());
                    timeline.setGraphLegend(is.getValues(), is.getName());
                    timeline.setGraphOutlineColor(is.getValues(), colorList[i]);
                    timeline.setGraphFillColor(is.getValues(), null);
                    timeline.setVerticalAxisLegendUnit(is.getValues(), is.getUnit());
                }
                i++;

            }

            if (normalized) {
                timeline.setVerticalAxisRange(0.0f, 1.0f);

            } else {
                timeline.setVerticalAxisRange(min, max);

            }

            timeline.requestRepaint();


            //updating the details bar

            this.detailsBar.removeAllComponents();

            for (IndicatorSeries is : series) {

                Panel infoBox = new Panel(is.getName());
                if (is.getStatus() == GPIValues.RED) {
                    infoBox.setStyleName("v-info-panel-red");
                } else {

                    if (is.getStatus() == GPIValues.YELLOW) {
                        infoBox.setStyleName("v-info-panel-yellow");
                    } else 
                    
                        if (is.getStatus() == GPIValues.GREEN) {
                        infoBox.setStyleName("v-info-panel-green");
                    } else {
                        infoBox.setStyleName("v-info-panel-gray");
                    }
                }

                infoBox.getContent().setStyleName("v-info-panel-content");

                infoBox.addComponent(new Label("Min value " + is.getMin(), Label.CONTENT_TEXT));
                infoBox.addComponent(new Label("Max value " + is.getMax(), Label.CONTENT_TEXT));
                infoBox.addComponent(new Label("Average value " + is.getAverage(), Label.CONTENT_TEXT));
                infoBox.addComponent(new Label("Current value " + is.getCurrent(), Label.CONTENT_TEXT));

                
                VerticalLayout vl = new VerticalLayout();
                
                vl.addComponent(infoBox);
                //vl.addComponent(new Label("Green [" + is.getNrl() + "-" + is.getNrh() + "]"));
                //vl.addComponent(new Label("Yellow [" + is.getNrh() + "-" + is.getErh() + ";" + is.getErl() + "-" + is.getNrl()+ "]"));
                //vl.addComponent(new Label("Red [<" + is.getErl() + ";>" + is.getErh() + "]"));
                
                vl.addComponent(new Label("Green [" + is.getNrl() + "-" + is.getNrh() + "]"));
                vl.addComponent(new Label("Yellow [" + is.getNrh() + "-" + is.getErh() + "]"));
                vl.addComponent(new Label("Red [>" + is.getErh() + "]"));
                
                vl.setExpandRatio(infoBox, 1.0f);
                
                
                this.detailsBar.addComponent(vl);
            }


        }
    }

    public void buttonClick(ClickEvent event) {

        this.normalized = cbNormalized.booleanValue();
        this.setSeries(this.series);

    }
    
   
}
